package cn.lizemin.controller;

import cn.lizemin.entity.User;
import cn.lizemin.service.UserService;
import cn.lizemin.utils.VerifyCodeUtils;
import com.sun.deploy.net.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yaml.snakeyaml.util.UriEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author lzm
 * @version 1.0
 * @description: TODO
 * @date 2022/1/2 11:42
 */
@Controller
@RequestMapping("user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @return
     */
    @RequestMapping("login")
    public String login(String username,String password,HttpSession session) throws UnsupportedEncodingException {
        log.debug("接收到的用户名：{}，接收到的密码：{}",username,password);
//        根据用户名查询用户
        try {
            User user = userService.login(username, password);
//            登录成功
            session.setAttribute("user",user);
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/login.jsp?msg=" + URLEncoder.encode(e.getMessage(),"UTF-8");
        }
//        重定向到另一个controller的handler
        return "redirect:/employee/list";
    }

    /**
     * 注册用户
     * @param user 用户
     * @param code 验证码
     * @param session 会话
     * @return url,页面
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("register")
    public String register(User user,String code,HttpSession session) throws UnsupportedEncodingException {
        log.debug("接收到的验证码：{}",code);
        log.debug("用户名：{}，真实姓名：{}，密码：{}，性别：{}",user.getUsername(),user.getRealname(),user.getPassword(),user.getGender());

        try {
//        1.比较验证码是否一致
            String sessionCode = session.getAttribute("code").toString();
            if (!sessionCode.equalsIgnoreCase(code)) throw new RuntimeException("验证码输入错误！");

//        2.注册用户
            userService.register(user);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return "redirect:/regist.jsp?msg=" + URLEncoder.encode(e.getMessage(),"UTF-8");
        }

        return "redirect:/login.jsp";
    }

    /**
     * 生成验证码
     */
    @RequestMapping("generateImgCode")
    public void generateImgCode(HttpSession session, HttpServletResponse response) throws IOException {
//        1.生成随机字符串
        String code = VerifyCodeUtils.generateVerifyCode(4);
//        2.将随机字符串保存到session中
        session.setAttribute("code",code);
//        3.将随机字符串生成图片
//        VerifyCodeUtils.outputImage();
//        4.通过response响应图片
        response.setContentType("image/png");//指定类型，方便浏览器解析
        ServletOutputStream os = response.getOutputStream();
        VerifyCodeUtils.outputImage(220,80,os,code);


    }


}
